#include <string>
#include <jni.h>
#include "LyTool.h"
#include "LyLogPrint.h"

void showJstring(JNIEnv *env, jstring msg) {
    const char *reslut = env->GetStringUTFChars(msg, JNI_FALSE);
    showLog("--->%s", reslut);
    // 释放字符数组
    env->ReleaseStringUTFChars(msg, reslut);
}


void showjlong(JNIEnv *env, const char *msg, jlong intValue) {
    char *buf = new char[100];
    sprintf(buf, "%s ,%lli", msg, intValue);
    showLog("--->%s", buf);
    free(buf);
}

std::string jstring2string(JNIEnv *env, jstring jStr) {
    const jclass stringClass = env->GetObjectClass(jStr);
    const jmethodID getBytes = env->GetMethodID(stringClass, "getBytes", "(Ljava/lang/String;)[B");
    const jstring utf = env->NewStringUTF("UTF-8");
    const jbyteArray stringJbytes = (jbyteArray) env->CallObjectMethod(jStr, getBytes, utf);

    size_t length = (size_t) env->GetArrayLength(stringJbytes);
    jbyte *pBytes = env->GetByteArrayElements(stringJbytes, NULL);

    std::string ret = std::string((char *) pBytes, length);

    env->DeleteLocalRef(stringClass);
    env->DeleteLocalRef(utf);
    env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_COMMIT);

    return ret;
}


/*std::string jstringToStdString(JNIEnv *env, jstring str) {
    int len = env->GetStringUTFLength(str);
    // 需要额外分配一个字符用于结尾的'\0'
    char *buf = new char[len + 1];
    env->GetStringUTFRegion(str, 0, len, buf);

    std::string result = std::string(buf);
    // 注意要手动释放内存
    delete[] buf;
    return result;
}*/







